package com.example.demo.config.shiro;

import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: yucsa
 * @Date: 2021/8/20 13:26
 * shiro配置类 共三个组件，两种实现方式，被注释掉的是最初的未添加缓存的实现方式。
 */

@Configuration
public class ShiroConfig {
    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);

        //添加内置过滤器
        Map<String,String> filterMap = new LinkedHashMap<>();
        /**
         * 将admin权限添加进过滤器
         * 注意url匹配规则，此处url错误是拦截失败的最主要原因
         */
        filterMap.put("/menu/admin/**","roles[admin]");
        filterMap.put("/user/admin/**","roles[admin]");
        filterMap.put("/order/admin/**","roles[admin]");
        filterMap.put("/admin/**","roles[admin]");
        filterMap.put("/cart/admin/**","roles[admin]");

        bean.setFilterChainDefinitionMap(filterMap);

        //设置无权限后自动返回的登录页面
        bean.setLoginUrl("/account/toLogin");
        //设置无权限页面
        bean.setUnauthorizedUrl("/noauth");
        return bean;
    }

    /**
     首次做的，注入UserRealm类的securityManager
     未使用缓存的
     */
//    @Bean(name="securityManager")
//    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        //关联UserRealm
//        securityManager.setRealm(userRealm);
//        return securityManager;
//    }

        @Bean(name="securityManager")
        public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customRealm") Realm customRealm){
            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
            //关联自定义Realm
            securityManager.setRealm(customRealm);
            return securityManager;
        }

        /**
         *    创建 realm 对象
         *    @yucs 2021/9/1
         *    自定义，用来使用缓存
         */
        @Bean(name="customRealm")
        public Realm customRealm(){
            CustomerRealm customerRealm = new CustomerRealm();

            //开启缓存管理--EhCacheManager是提供的已实现类
    //        customerRealm.setCacheManager(new EhCacheManager());
            customerRealm.setCacheManager(new RedisCacheManager());
            //开启全局缓存
            customerRealm.setCachingEnabled(true);
            //认证缓存
            customerRealm.setAuthenticationCachingEnabled(true);
            customerRealm.setAuthenticationCacheName("authenticationCache_认证缓存");
            //授权缓存
            customerRealm.setAuthorizationCachingEnabled(true);
            customerRealm.setAuthorizationCacheName("authorizationCache_授权缓存");

            return customerRealm;

        }

    /**
     * 最初版本的Realm，不使用缓存
     * @return
     */
//    @Bean(name="userRealm")
//    public UserRealm userRealm(){
//        return new UserRealm();
//    }


}